local liaowei = fk.CreateSkill{
  name = "hanqing__liaowei",
}

Fk:loadTranslationTable{
  ["hanqing__liaowei"] = "燎围",
  [":hanqing__liaowei"] = "每回合限两次，你可以弃置一张手牌，视为使用一张【过河拆桥】/【火攻】，然后此牌目标可以视为对你使用"..
  "另一张牌并复原此技能。最后，你可以使用因此弃置的牌。",

  ["#hanqing__liaowei"] = "燎围：你可以弃置一张手牌，视为使用【过河拆桥】或【火攻】，目标可以视为对你使用另一张牌",
  ["#hanqing__liaowei-ask"] = "燎围：是否视为对 %src 使用【%arg】？",
  ["#hanqing__liaowei-use"] = "燎围：你可以使用这些牌",

  ["$hanqing__liaowei1"] = "青天临之，皎日照之！吾今誓众，有死无二！",
  ["$hanqing__liaowei2"] = "群鸟或众，五牛扬旌。好儿郎，可往矣！",
}

liaowei:addEffect("viewas", {
  anim_type = "offensive",
  pattern = "dismantlement,fire_attack",
  prompt = "#hanqing__liaowei",
  times = function(self, player)
    return 2 - player:usedSkillTimes(liaowei.name, Player.HistoryTurn)
  end,
  interaction = function(self, player)
    local all_names = {"dismantlement", "fire_attack"}
    local names = player:getViewAsCardNames(liaowei.name, all_names)
    if #names == 0 then return end
    return UI.CardNameBox { choices = names, all_choices = all_names }
  end,
  card_filter = function (self, player, to_select, selected)
    return #selected == 0 and table.contains(player:getCardIds("h"), to_select) and not player:prohibitDiscard(to_select)
  end,
  view_as = function(self, player, cards)
    if #cards ~= 1 or self.interaction.data == nil then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = liaowei.name
    card:addFakeSubcards(cards)
    return card
  end,
  before_use = function (self, player, use)
    local room = player.room
    use.extra_data = use.extra_data or {}
    use.extra_data.hanqing__liaowei = player.id
    room:throwCard(use.card.fake_subcards, liaowei.name, player, player)
  end,
  after_use = function (self, player, use)
    local room = player.room
    if player.dead then return end
    local name = use.card.name == "dismantlement" and "fire_attack" or "dismantlement"
    local card = Fk:cloneCard(name)
    card.skillName = liaowei.name
    local targets = table.filter(use.tos, function (p)
      return not p.dead and p:canUseTo(card, player)
    end)
    if #targets > 0 then
      room:sortByAction(targets)
      for _, p in ipairs(targets) do
        if player.dead then return end
        if room:askToSkillInvoke(p, {
          skill_name = liaowei.name,
          prompt = "#hanqing__liaowei-ask:"..player.id.."::"..name,
        }) then
          player:setSkillUseHistory(liaowei.name, 0, Player.HistoryTurn)
          room:useVirtualCard(name, nil, p, player, liaowei.name)
        end
      end
      if player.dead then return end
    end
    local cards = table.simpleClone(use.card.fake_subcards)
    local effect_event = room.logic:getEventsByRule(GameEvent.CardEffect, 1, function (e)
      return e.data.card == use.card
    end, nil, Player.HistoryTurn)
    if effect_event then
      effect_event[1]:searchEvents(GameEvent.MoveCards, 1, function (e)
        for _, move in ipairs(e.data) do
          if move.moveReason == fk.ReasonDiscard then
            for _, info in ipairs(move.moveInfo) do
              table.insertIfNeed(cards, info.cardId)
            end
          end
        end
      end)
    end
    cards = table.filter(cards, function (id)
      return table.contains(room.discard_pile, id)
    end)
    while #cards > 0 and not player.dead do
      local new_use = room:askToUseRealCard(player, {
        pattern = cards,
        skill_name = liaowei.name,
        prompt = "#hanqing__liaowei-use",
        extra_data = {
          bypass_times = true,
          extraUse = true,
          expand_pile = cards,
        }
      })
      if new_use then
        cards = table.filter(cards, function (id)
          return table.contains(room.discard_pile, id)
        end)
        table.removeOne(cards, new_use.card.id)
      else
        break
      end
    end
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(liaowei.name, Player.HistoryTurn) < 2
  end,
  enabled_at_response = function(self, player, response)
    return not response and player:usedSkillTimes(liaowei.name, Player.HistoryTurn) < 2
  end,
})

liaowei:addAI(nil, "vs_skill")

return liaowei
